/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.suai.main;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.suai.manager.StatisticManager;
import org.suai.system.MainSystem;
import org.suai.system.QueueSystem;
import org.suai.system.impl.AlohaSystemImpl;
import org.suai.system.impl.BlockSystemImpl;
import org.suai.system.impl.DummySystem;
import org.suai.system.impl.MainSystemImpl;
import org.suai.system.impl.PackageGeneratorRing;
import org.suai.system.impl.PackageGeneratorSingle;

/**
 *
 * @author awm
 */
public class Main {
    final static String LAMDA = "-l";
    final static String USERS = "-n";
    final static String TIMES = "-t";
    final static String SYSTEMS = "-s";
    public static void main(String[] args) {
        args = new String[]{"-n", "10", "-l", "0.5", "-s", "b", "-t", "10000"};
        System.out.println("Start system...");

        boolean isAloha = false;
        boolean isBlock = false;
        double lamda = 0.0;
        int userCount = 0;
        int experimentCount = 0;

        if(args.length < 8) {
            System.out.println("Usage: -l <lamda> -n <user count> -t <work time> -s a[b]" );
            return;
        }

        for(int i = 0; i < args.length; i++) {
            if( LAMDA.equalsIgnoreCase(args[i]) && i < (args.length - 1) ) {
                lamda = Double.parseDouble(args[++i]);
            } else if ( USERS.equalsIgnoreCase(args[i]) && i < (args.length - 1) ) {
                userCount = Integer.parseInt(args[++i]);
            } else if ( TIMES.equalsIgnoreCase(args[i]) && i < (args.length - 1) ) {
                experimentCount = Integer.parseInt(args[++i]);
            } else if ( SYSTEMS.equalsIgnoreCase(args[i]) && i < (args.length - 1) ) {
                i++;
                if(args[i].contains("a")) isAloha = true;
                if(args[i].contains("b")) isBlock = true;
            }
        }

        if( lamda <= 0.0 || userCount <= 0 || !(isAloha || isBlock) ) {
            System.out.println("Wrong values");
            System.out.println("Usage: -l <lamda> -n <user count> -s a[b]" );
            return;
        }

        System.out.println("lamda: " + lamda + "\nuser count: " + userCount + "\ntime: " + experimentCount);
        System.out.println("Aloha system: " + isAloha + "\nBlock system: " + isBlock);

        MainSystem mainSystem = null;

        if( isAloha ) {
            StatisticManager.getInstance().restoreStats();
            System.out.println("Run Aloha...");
            //create systems
            List listAlohaSystems = new LinkedList(
                    Arrays.asList(
                        new AlohaSystemImpl(0, lamda, userCount),
                        new AlohaSystemImpl(1, lamda, userCount),
                        new AlohaSystemImpl(2, lamda, userCount),
                        new AlohaSystemImpl(3, lamda, userCount)
                    )
            );


            mainSystem = new MainSystemImpl(listAlohaSystems, new PackageGeneratorRing());

            for(int i = 0; i < experimentCount; i++) {
                mainSystem.doStep();
            }

            System.out.println(StatisticManager.getInstance());

//            StatisticManager.getInstance().restoreStats();
//
//            listAlohaSystems = new LinkedList(
//                    Arrays.asList(
//                        new AlohaSystemImpl(0, lamda, userCount),
//                        new DummySystem(1, 0.0, 0),
//                        new AlohaSystemImpl(2, lamda, userCount),
//                        new DummySystem(3, 0.0, 0)
//                    )
//            );
//
//
//            mainSystem = new MainSystemImpl(listAlohaSystems, new PackageGeneratorRing());
//
//            for(int i = 0; i < experimentCount; i++) {
//                mainSystem.doStep();
//            }
//
//            System.out.println(StatisticManager.getInstance());
        }

        if( isBlock ) {
            StatisticManager.getInstance().restoreStats();
            System.out.println("Run Block...");
            //create systems
            List listBlockSystems = new LinkedList(
                    Arrays.asList(
                        new BlockSystemImpl(0, lamda, userCount),
                        new DummySystem(1, lamda, userCount),
                        new BlockSystemImpl(2, lamda, userCount),
                        new DummySystem(3, lamda, userCount)
                    )
            );


            mainSystem = new MainSystemImpl(listBlockSystems, new PackageGeneratorRing());

            for(int i = 0; i < experimentCount; i++) {
                mainSystem.doStep();
                if(i % 500 == 0) {
                    System.out.println(i);
                }
            }

            System.out.println(StatisticManager.getInstance());
        }
    }
}
